﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data;

namespace Model
{
    public class Matrix
    {
        private DataTable dt {get;set;}
        public List<In_Matrix_Cell> cells { get; set; }
        public Matrix()
        {

        }
        public Matrix(List<Brand> b,List<Catalog> c)
        {

            dt = new DataTable("matrix_data");
            dt.Columns.Add("b_tag");
            c.ForEach(k =>
                {
                    DataColumn col = new DataColumn();
                    col.ColumnName = k.ID.ToString();
                    col.Caption = k.title_en;
                    if (!dt.Columns.Contains(k.ID.ToString()))
                    {
                        dt.Columns.Add(col);
                    }
                });

            cells = new List<In_Matrix_Cell>();
            b.ForEach(x =>
                {
                    DataRow row = dt.NewRow();
                    row["b_tag"] = x.title_origin;

                    In_Matrix_Cell cell = new In_Matrix_Cell()
                    {
                        brand = x
                    };
                    cell.catalog_axis = new Dictionary<string,Catalog_Axis>();
                    c.ForEach(k =>
                        {
                            Catalog_Axis axis = new Catalog_Axis()
                            {
                                catalog = k,
                                exist = true,
                                isFeatured = false,
                                TD = 0,
                                LD = 0,
                                TP = 0,
                                LP = 0
                            };
                            cell.catalog_axis.Add(axis.catalog.ID.ToString(), axis);
                            row[axis.catalog.ID.ToString()] = axis.value;
                        });
                    cells.Add(cell);
                    dt.Rows.Add(row);
                });
        }

        public DataTable data
        {
            get
            {
                try
                {
                    dt.Rows.Clear();
                    cells.ForEach(c =>
                        {
                            DataRow row = dt.NewRow();
                            row["b_tag"] = c.b_tag;
                            for (int i = 1; i < dt.Columns.Count; i++)
                            {
                                row[dt.Columns[i].ColumnName] = c.catalog_axis[dt.Columns[i].ColumnName].value;
                            }
                            dt.Rows.Add(row);
                        });
                    return dt;
                }
                catch(Exception ex)
                {
                    return null;
                }
            }
        }

        public string post_matrix
        {
            get
            {
                string post_str = "";
                cells.ForEach(c =>
                    {
                        foreach(Catalog_Axis axis in c.catalog_axis.Values)
                        {
                            post_str += string.Format("{0},{1},{2}|", c.brand.ID, axis.catalog.ID, axis.value);
                        }
                    });
                post_str = post_str.TrimEnd('|');
                return post_str;
            }
        }
    }

    public class In_Matrix_Cell
    {
        public Brand brand { get; set; }
        
        public string b_tag
        {
            get
            {
                return brand.title_origin;
            }
        }

        public Dictionary<string,Catalog_Axis> catalog_axis { get; set; }
    }

    public class Catalog_Axis
    {
        public Catalog catalog { get; set; }

        public bool isFeatured { get; set; }
        public bool exist { get; set; }
        public float TD { get; set; }
        public float LD { get; set; }
        public float TP { get; set; }
        public float LP { get; set; }

        public string value
        {
            get
            {
                return string.Format("{0},{1},{2},{3},{4},{5}",exist ? 1 : 0,isFeatured ? 1 : 0, TD, LD, TP, LP);
            }
        }
    }
}
